//
//  CYWenDaDetail_HuiFuTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/27.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYWenDaDetail_HuiFuTableViewCell.h"

@implementation CYWenDaDetail_HuiFuTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.height / 2;
    self.headImageView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeadImageView:)];
    [self.headImageView addGestureRecognizer:tap];
    self.headImageView.userInteractionEnabled = YES;
}

- (void)tapHeadImageView:(UITapGestureRecognizer *)tap{
    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    zhuye.userId = self.model.user_id;
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.nicknameLabel.text = model.nickname;
    self.timeLabel.text = model.created_time;
    self.introLabel.text = [[Tool sharedInstance] delHTMLBiqoQianWithHtml:model.content];
    
    [self.zanBtn setTitle:model.like_number forState:UIControlStateNormal];
    [self.commentBtn setTitle:model.comment_number forState:UIControlStateNormal];
    if ([self.model.is_like integerValue] == 1) {
        self.zanBtn.selected = YES;
    }else{
        self.zanBtn.selected = NO;
    }
}

- (IBAction)zanAction:(UIButton *)sender {
    if ([self.model.is_like integerValue] == 1) {
        [self cancleZanWenZhang];
    }else{
        [self zanWenZhang];
    }
}

- (IBAction)pinglunAction:(UIButton *)sender {
    
}

#pragma mark - 给回答赞和取消点赞
- (void)zanWenZhang{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"addLike"];
    NSDictionary *param = @{@"type":@1, @"likeId":self.model.ID, @"origin":@"4", @"toLikerId":self.model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"给回答点赞 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_like = [NSNumber numberWithInteger:1];
                NSInteger tag = [self.model.like_number integerValue];
                self.model.like_number = [NSString stringWithFormat:@"%ld",(tag + 1)];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}
- (void)cancleZanWenZhang{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"cancleLike"];
    NSDictionary *param = @{@"type":@1, @"articleId":self.model.ID, @"origin":@"4", @"toLikerId":self.model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"给回答取消点赞 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_like = [NSNumber numberWithInteger:2];
                NSInteger tag = [self.model.like_number integerValue];
                self.model.like_number = [NSString stringWithFormat:@"%ld",(tag - 1)];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}
@end
